package com.myproject.myspringboot.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.myproject.myspringboot.pojo.ResponeBean;
import com.myproject.myspringboot.pojo.ReturnPageVo;
import com.myproject.myspringboot.pojo.User;
import com.myproject.myspringboot.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author hushuhua
 * @date 2020/2/20 14:52
 * @discription
 */
@Api("测试用户接口")
@RequestMapping("/user")
@RestController
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;
    //查询全部用户；
    @ApiOperation("查询全部用户")
    @RequestMapping("/findUser")
    public ReturnPageVo findUsers(@RequestParam("limit") int limit, @RequestParam("page") int page) {
        //分页实行;
        Page<Object> pager = PageHelper.startPage(page, limit);
        List<User> users = userService.findUsers();
        log.info("业务层获取的数据集合{}",users);
        return new ReturnPageVo("请求获取数据成功",0,pager.getTotal(),users);
    }

    //新增用户
    @ApiOperation("新增用户")
    @RequestMapping("/addUser")
    public ResponeBean addUser(@RequestBody User user) {
        return userService.addUser(user);
    }

    //根据id修改用户；
    @ApiOperation("根据id修改用户")
    @RequestMapping("/updateById")
    public ResponeBean updateById(@RequestBody User user) {
        return userService.updateById(user);
    }

    //根据id删除用户；
    @ApiOperation("根据id删除用户")
    @RequestMapping("/delById")
    public ResponeBean delById(String id) {
        return userService.delById(id);
    }

    //把新增数据导出到excel表；
    @RequestMapping("/export")
    public ResponeBean export() {
        List<List<String>> lists = new ArrayList<>();
        //获取所有用户集合；
        List<User> users = userService.findUsers();
        for (User user : users) {
            List<String> list = new ArrayList<>();
            list.add(user.getId() + "");
            list.add(user.getUsername());
            list.add(user.getPassword());
            list.add(user.getMobile());
            lists.add(list);
        }

        //这里需要注意一个点：要创建什么类型的excel表，需要new对应的wb;
        //比如：xls--HSSFWorkbook   xlsx--XSSFWorkbook

        //第一步，创建一个workbook对应一个excel文件
        XSSFWorkbook workbook = new XSSFWorkbook();
        //第二部，在workbook中创建一个sheet对应excel中的sheet
        XSSFSheet sheet = workbook.createSheet("导入数据测试表");
        //第三部，在sheet表中添加表头第0行，老版本的poi对sheet的行列有限制
        XSSFRow row = sheet.createRow(0);
        //第四步，创建单元格，设置表头
        XSSFCell cell = row.createCell(0);
        cell.setCellValue("id");
        cell = row.createCell(1);
        cell.setCellValue("姓名");
        cell = row.createCell(2);
        cell.setCellValue("密码");
        cell = row.createCell(3);
        cell.setCellValue("电话号码");

        //第五步，写入数据
        for (int i = 0; i < lists.size(); i++) {

            List<String> oneData = lists.get(i);
            XSSFRow row1 = sheet.createRow(i + 1);
            for (int j = 0; j < oneData.size(); j++) {

                //创建单元格设值
                row1.createCell(j).setCellValue(oneData.get(j));
            }
        }

        //将文件保存到指定的位置
        try {
            /*File file1 = new File("D:/myspringboot/test/导入测试表.xlsx");
            //判断file1文件是否存在,不存在就创建
            if (!file1.exists()) {
                if (file1.mkdirs()) {
                    log.info("创建文件夹失败");
                }*/
                FileOutputStream fos = new FileOutputStream("D:\\springboot\\test\\导入测试表.xlsx");
                workbook.write(fos);
                System.out.println("写入成功");
                fos.close();
        } catch(Exception e){
                e.printStackTrace();
            }
        return new ResponeBean(200, "全部用户导入excel成功！", "");
    }
}
